package dao;

import entity.Owner;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

public class OwnerDAO extends DAO<Owner> {

    public OwnerDAO(EntityManagerFactory emf) {
        super(emf);
    }

    @Override
    protected Root from(CriteriaQuery cq) {
        return cq.from(Owner.class);
    }

    @Override
    protected boolean exists(Owner object) {
        EntityManager entityManager = getEntityManager();
        Query query = entityManager.createQuery("SELECT o FROM Owner o WHERE o.name = :name AND o.lastname = :lastname");
        query.setParameter("name", object.getName());
        query.setParameter("lastname", object.getLastname());
        
        return !query.getResultList().isEmpty();
    }

}
